
/**   
 * @Title: StringUtil.java 
 * @Package: com.rename.icode.utils 
 * @Description: Administrator版权所有
 * @author Administrator  
 * @date 2015-2-11 下午4:42:15 
 * @version  
 */


package com.rename.icode.utils;

/** 
 * @Description 实现了...功能
 * @author Administrator
 * @date 2015-2-11 下午4:42:15 
 * @version V1.0
 */

public class StringUtil {
	public static String doEmpty(String str) {
		return doEmpty(str, "");
	}

	/**
	 * 锟斤拷锟斤拷锟斤拷址锟斤拷锟�
	 * 
	 * @param str
	 * @param defaultValue
	 * @return String
	 */
	public static String doEmpty(String str, String defaultValue) {
		if (str == null || str.equalsIgnoreCase("null")
				|| str.trim().equals("") || str.trim().equals("锟斤拷锟斤拷选锟斤拷")) {
			str = defaultValue;
		} else if (str.startsWith("null")) {
			str = str.substring(4, str.length());
		}
		return str.trim();
	}

	/**
	 * 锟斤拷选锟斤拷
	 */
	final static String PLEASE_SELECT = "锟斤拷选锟斤拷...";

	public static boolean notEmpty(Object o) {
		return o != null && !"".equals(o.toString().trim())
				&& !"null".equalsIgnoreCase(o.toString().trim())
				&& !"undefined".equalsIgnoreCase(o.toString().trim())
				&& !PLEASE_SELECT.equals(o.toString().trim());
	}

	public static boolean empty(Object o) {
		return o == null || "".equals(o.toString().trim())
				|| "null".equalsIgnoreCase(o.toString().trim())
				|| "undefined".equalsIgnoreCase(o.toString().trim())
				|| PLEASE_SELECT.equals(o.toString().trim());
	}

	public static boolean num(Object o) {
		int n = 0;
		try {
			n = Integer.parseInt(o.toString().trim());
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		if (n > 0) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean decimal(Object o) {
		double n = 0;
		try {
			n = Double.parseDouble(o.toString().trim());
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		if (n > 0.0) {
			return true;
		} else {
			return false;
		}
	}
	
	public static boolean isNumeric(String str){
		for (int i = str.length(); --i >= 0;) {   
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 锟斤拷JID锟斤拷锟斤拷锟矫伙拷锟斤拷
	 * 
	 * @param Jid
	 * @return
	 */
	public static String getUserNameByJid(String Jid) {
		if (empty(Jid)) {
			return null;
		}
		if (!Jid.contains("@")) {
			return Jid;
		}
		return Jid.split("@")[0];
	}

	/**
	 * 锟斤拷锟矫伙拷锟斤拷锟斤拷锟斤拷JID
	 * 
	 * @param jidFor
	 *            锟斤拷锟斤拷//锟斤拷ahic.com.cn
	 * @param userName
	 * @return
	 */
	public static String getJidByName(String userName, String jidFor) {
		if (empty(jidFor) || empty(jidFor)) {
			return null;
		}
		return userName + "@" + jidFor;
	}

	/**
	 * 锟斤拷锟矫伙拷锟斤拷锟斤拷锟斤拷JID,使锟斤拷默锟斤拷锟斤拷锟斤拷ahic.com.cn
	 * 
	 * @param userName
	 * @return
	 */
//	public static String getJidByName(String userName) {
//		Server srv = AppConfigManager.getInstance().getServer();
//
//		return getJidByName(userName, srv.getXmppServiceName());
//	}

	/**
	 * 锟斤拷锟矫伙拷锟斤拷锟斤拷锟斤拷锟街伙拷锟斤拷
	 * 
	 * @param userName
	 * @return
	 */
//	public static String getCellphoneByName(String userName) {
//		if (empty(userName)) {
//			return null;
//		}
//		return userName.split(User.NAME_PREFIX)[1];
//	}
	
	/**
	 * 锟斤拷锟斤拷锟街伙拷锟斤拷锟斤拷锟斤拷锟矫伙拷锟斤拷
	 * 
	 * @param userName
	 * @return
	 */
//	public static String getJidByCellphone(String cellphone) {
//		if (empty(cellphone)) {
//			return null;
//		}
//		String name = getNameByCellphone(cellphone);
//		
//		return getJidByName(name);
//	}
	
	/**
	 * 锟斤拷锟斤拷锟街伙拷锟斤拷锟斤拷锟斤拷锟矫伙拷锟斤拷
	 * 
	 * @param userName
	 * @return
	 */
//	public static String getNameByCellphone(String cellphone) {
//		if (empty(cellphone)) {
//			return null;
//		}
//		return User.NAME_PREFIX + cellphone;
//	}
	
//	public static String getImJidByVoipUserName(String voipUserName) {
//		if (empty(voipUserName)) {
//			return null;
//		}
//		Server srv = AppConfigManager.getInstance().getServer();
//
//		return User.NAME_PREFIX + voipUserName.split("@")[0] + srv.getXmppServiceName();
//	}
	
	/**
	 * 锟斤拷锟捷革拷锟斤拷锟斤拷时锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 锟斤拷 时 锟斤拷 锟斤拷
	 * 
	 * @param allDate
	 *            like "yyyy-MM-dd hh:mm:ss SSS"
	 * @return
	 */
	public static String getMonthTomTime(String allDate) {
		return allDate.substring(5, 19);
	}

	/**
	 * 锟斤拷锟捷革拷锟斤拷锟斤拷时锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 锟斤拷 时 锟斤拷 锟铰碉拷锟斤拷锟斤拷
	 * 
	 * @param allDate
	 *            like "yyyy-MM-dd hh:mm:ss SSS"
	 * @return
	 */
	public static String getMonthTime(String allDate) {
		return allDate.substring(5, 16);
	}
	
//	public static String genMessageUniqueId(IMMessage msg) {
//		return String.valueOf(msg.hashCode()) + String.valueOf(DateUtil.getCurDateLong());
//	}
	
//	public static String genBindingID() {
//		return ContacterManager.getInstance().getUserMe().getName() + "@" + DateUtil.getCurDateLong();
//	}
	
	// 将两个ASCII字符合成一个字节； 如："EF"--> 0xEF
	public static byte uniteBytes(byte src0, byte src1) {
		byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
				.byteValue();
		_b0 = (byte) (_b0 << 4);
		byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
				.byteValue();
		byte ret = (byte) (_b0 ^ _b1);
		return ret;
	}

	// 将指定字符串src，以每两个字符分割转换为16进制形式 如："2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF,
	public static byte[] HexString2Bytes(String src) {
		byte[] ret = new byte[src.length() / 2];
		byte[] tmp = src.getBytes();
		for (int i = 0; i < ret.length; i++) {
			ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
		}
		return ret;
	}
	
	// 字节数组转十六进制字符串
	public static String getHexString(byte[] b){
		if(b == null) {
			return null;
		}
		
		String result = "";
		for (int i = 0; i < b.length; i++) {
			result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
		}
		return result;
	}
	
	public static String getNameByGroupName(String groupName) {
		return groupName.split("_")[0];
	}
	
	public static String getTimeByGroupName(String groupName) {
		return groupName.split("_")[1];
	}
	
//	public static String packGroupName(String groupName) {
//		if (groupName == null || groupName.length() == 0) {
//			return null;
//		}
//		
//		String userName = getNameByGroupName(groupName);
//		String createTime = getTimeByGroupName(groupName);
//		
//		userName.hashCode();
//		StringBuffer sb = new StringBuffer();
//		sb.append(getCellphoneByName(userName));
//		sb.append(createTime.substring(8));
//		
//		return sb.toString();
//	}
	
	
	public static String getNameByAKeyUserId(String userId) {
		if (userId == null || userId.length() == 0) {
			return null;
		}
		
		byte[] names = HexString2Bytes(userId.substring(0, 15));
		
		String name = new String(names);
		return name.trim();
	}
	
//	public static String getAKeyUserIdByName(String name) {
//		if (name == null || name.length() == 0) {
//			return null;
//		}
//		
//		StringBuffer sb = new StringBuffer();
//		String packedName = null;
//		if (name.contains("_")) {
//			// packed: group owner(cellphone) + last 5 bytes of createTime
//			packedName = packGroupName(name);
//		}
//		else {
//			// packed: user cellphone
//			packedName = getCellphoneByName(name);
//		}
//		
//		
//		String names = getHexString(packedName.getBytes());
////		for(Byte b : packedName.getBytes())
////			names += Integer.toHexString(b);
//		
//		sb.append(names);
//		int len = sb.length();			
//		while(len < 128){
//			sb.append("00");
//			len += 2;
//		}
//
//		return sb.toString();
//	}
	
	public static String printableBytes(byte[] bytes) {
		StringBuffer sb = new StringBuffer();
		int count = 0;
		for(int i = 0; i < bytes.length; i++){
			sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1) + " ");
			count += 1;
			if (count == 16) {
				sb.append("\r\n");
				count = 0;
			}
		}
		return sb.toString();
	}

	 
	/** 
	 * @Description 实现了...功能
	 * @author Administrator
	 * @param trim
	 * @return  
	 */
	  	
	public static String getNameByCellphone(String cellphone) {
		return cellphone;
	}
}
